package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import one.stand.model.CompanyCouponModel;
import one.stand.model.ResultModel;
import one.stand.model.UserCouponModel;
import one.stand.request.CompanyCouponRequest;
import one.stand.request.coupon.CouponQuery;
import one.stand.response.CompanyCouponResponse;
import one.stand.service.CompanyCouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: QIK
 * @CreateDate: 2019/6/25 9:23
 */
@Service
@Transactional
public class CompanyCouponServiceImpl extends PublicBaseService implements CompanyCouponService {
    @Override
    public ResultModel<List<CompanyCouponResponse>> selectGoodsTypeAndCompanyNameByCompanyId(CouponQuery request) {
        if(null==request.getCompanyId()){
            return ResultModel.checkParamFail("请求参数companyId为空");
        }
        if(request.getCouponType() == null){
            return ResultModel.checkParamFail("请求参数类型错误");
        }
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<CompanyCouponModel> outModelList = companyCouponMapper.selectGoodsTypeAndCompanyNameListByCompanyId(request);
        if(outModelList.size()<=0){
            return ResultModel.noData();
        }
        List<CompanyCouponResponse> responseList=new ArrayList<>();
        outModelList.forEach(ml->{
            CompanyCouponResponse response = new CompanyCouponResponse();
            BeanUtils.copyProperties(ml,response);

            response.setReceidved(Optional.ofNullable(this.userCouponMapper.queryUserCoupon(new UserCouponModel(){{
                setCouponId(response.getCouponId());
                setUserId(request.getUserId());
            }})).isPresent());

            responseList.add(response);
        });
        return ResultModel.success(responseList);
    }
}
